package com.itheima.thread04;

//消费者
public class Foodie implements Runnable {
    private Desk desk;

    public Foodie(){}

    public Foodie(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {
        while (true){
            synchronized (desk.getLock()){
                if (desk.getCount() == 0){
                    //数量为0就退出
                    break;
                }else {
                    if (desk.isFlag()){
                        //有，就吃
                        System.out.println("吃货在吃汉堡包");
                        //吃完，更改共享数据为false，表示桌上没有汉堡了
                        desk.setFlag(false);
                        //将还可以吃汉堡的数量-1
                        desk.setCount(desk.getCount()-1);
                        //叫醒cooker去做汉堡
                        desk.getLock().notifyAll();
                    }else {
                        //没有就等待
                        try {
                            desk.getLock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
